package com.hnucm.cooklife.model;

import android.util.Log;

import com.blankj.utilcode.util.NetworkUtils;
import com.hnucm.cooklife.base.BaseModel;
import com.hnucm.cooklife.bean.db.DishDetail;
import com.hnucm.cooklife.bean.db.Origin;
import com.hnucm.cooklife.bean.db.Step;
import com.hnucm.cooklife.bean.detail.DishDetailData;
import com.hnucm.cooklife.bean.me.ToAddCollectData;
import com.hnucm.cooklife.bean.uploaddish.UpRece;
import com.hnucm.cooklife.contract.dishdetail.Contract;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;

public class DishDetailModel extends BaseModel implements Contract.IDishDetailModel {
//    public DishDetailDetailModel() {
//        setCookies(false);
//    }

    @Override
    public Observable<DishDetail> loadDishDetail(Integer dishId) {
//        Observable<List<DishDetail>> loadFromLocal = Observable.create(emitter -> {
//            List<DishDetail> DishDetailList = LitePal.findAll(DishDetail.class);
//            emitter.onNext(DishDetailList);
//            emitter.onComplete();
//        });

//        if(NetworkUtils.isConnected()){
//            Observable<DishDetail> loadFromNet = loadDishDetailFromNet();
////            return Observable.concat(loadFromLocal,loadFromNet);
//            return loadFromNet;
////        } else {
//////            return loadFromLocal;
////        }
//    }


        return  mApiServer.loadDishDetail(dishId)
                .map(dishDetailData -> {
                    DishDetailData.DataDTO dto = dishDetailData.getData();
                    DishDetail dishDetail = new DishDetail();
                    dishDetail.dishId = dto.getDishId();
                    dishDetail.dishname = dto.getDishname();
                    dishDetail.dishpic = dto.getDishpic();
                    dishDetail.dishdesc = dto.getDishdesc();
                    dishDetail.userid = dto.getUserid();
                    dishDetail.username = dto.getUsername();
                    dishDetail.icon = dto.getIcon();
                    dishDetail.originList.addAll(dto.getOriginList());
                    dishDetail.stepList.addAll(dto.getStepList());
//                    dishDetail.workList.addAll(dto.getWorkList());
                    return dishDetail;
                });
    }

    @Override
    public Observable<ToAddCollectData> collect(int userId, int dishId) {
        return mApiServer.addCollect(userId, dishId);
    }

    @Override
    public Observable<ToAddCollectData> unCollcet(int userId, int dishId) {
        return mApiServer.unCollect(userId, dishId);
    }
//    //加载并更新本地的DishDetail
//    private Observable<List<DishDetail>> loadDishDetailFromNet(){
//        return mApiServer.loadDishDetail()
//                .map(DishDetail -> {
//                    List<DishDetail> DishDetailList = new ArrayList<>();
//                    DishDetail.getData().stream().forEach(dataDTO -> {
//                        DishDetail DishDetailDB = new DishDetail();
//                        DishDetailDB.dishId = dataDTO.getDishId();
//                        DishDetailDB.dishName = dataDTO.getDishName();
//                        DishDetailDB.dishDesc = dataDTO.getDishDesc();
//                        DishDetailDB.dishAuid = dataDTO.getDishAuid();
//                        DishDetailDB.dishAuthor = dataDTO.getDishAuthor();
//                        DishDetailDB.dishPic = dataDTO.getDishPic();
//                        List<Origin> originList = dataDTO.getOriginList();
//                        List<Step> stepList = dataDTO.getStepList();
//                        DishDetailList.add(DishDetailDB);
//                    });
//                    if(DishDetailList.size() > 0){
//                        LitePal.deleteAll(DishDetail.class);
//                    }
//                    LitePal.saveAll(DishDetailList);
//                    return DishDetailList;
//                });
//    }
//
//    @Override
//    public Observable<List<DishDetail>> refreshDishDetail() {
//        return loadDishDetailFromNet();
//    }

    @Override
    public Observable<DishDetail> refreshDishDetail(Integer dishId) {
        return loadDishDetail(dishId);
    }

    @Override
    public Observable<UpRece> updateHistory(int dishId, int userId) {
        return mApiServer.updateHistory(dishId, userId);
    }
}
